package com.shop.user.controller;


import com.shop.common.pojo.RD;
import com.shop.common.pojo.SpManager;
import com.shop.user.mapper.UserMapper;
import com.shop.user.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.SourceVersion;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
/*@RequestMapping("/user")*/
public class UserController {
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("/query/{id}")
    public SpManager query(@PathVariable Integer id){
        System.out.println(9001);
        return userService.findById(id);
    }
    @RequestMapping("query")
    public String findServerPort(){
        System.out.println(9001);
        return serverPort;
    }
    @GetMapping("users")
    public RD findAll(@RequestParam(value = "query",defaultValue = "") String query,@RequestParam(value = "pagenum",defaultValue = "1") int pagenum,@RequestParam(value = "pagesize",defaultValue = "2") int pagesize){
        Map map1=new HashMap<>();
        Map map2=new HashMap<>();
        int totalpage=0;
        List<SpManager> list= null;
        try {
            list = userService.findAndLimit(query,pagenum,pagesize);
            int totallist=userService.findNoLimit(query);
            totalpage=totallist%pagesize==0?totallist/pagesize:totallist/pagesize+1;
            map1.put("totalpage",totalpage);
            map1.put("pagenum",pagenum);
            map1.put("users",list);
            map2.put("msg","获取成功");
            map2.put("status",200);
        } catch (Exception e) {
            map1.put("totalpage",totalpage);
            map1.put("pagenum",pagenum);
            map1.put("users",null);
            map2.put("msg","获取失败");
            map2.put("status",201);
        }
        RD rd=new RD(map1,map2);
        return rd;
    }
    @PostMapping("users")
    public RD addUser(@RequestBody SpManager spManager)
    {
        Map map1=new HashMap<>();
        Map map2=new HashMap<>();
        spManager.setMgTime(System.currentTimeMillis() / 1000);
        System.out.println("插入前"+spManager);
        try {
        int ok=userMapper.addUser(spManager);
        System.out.println("插入后"+spManager);
        map1.put("data",spManager);
        map2.put("msg","添加成功");
        map2.put("status",200);
        } catch (Exception e) {
            map1.put("data",null);
            map2.put("msg","添加失败");
            map2.put("status",201);

        }
        RD rd=new RD(map1,map2);
        return rd;
    }
    @PutMapping("/users/{uId}/state/{type}")
    public RD updataUser(@PathVariable int uId,@PathVariable int type)
    {
        Map map1=new HashMap<>();
        Map map2=new HashMap<>();
       /* SpManager spManager=userMapper.selectById(uId);
        spManager.setMgState((byte) type);*/
        try {
            userMapper.updataUserStatuts(uId,type);
            SpManager spManager=userMapper.selectById(uId);
            map1.put("data",spManager);
            map2.put("msg","添加成功");
            map2.put("status",200);
        } catch (Exception e) {
            map1.put("data",null);
            map2.put("msg","添加失败");
            map2.put("status",201);
        }
        RD rd=new RD(map1,map2);
        return rd;
    }
    @GetMapping("/users/{id}")
    public RD selectUserById(@PathVariable int id)
    {
        Map map1=new HashMap<>();
        Map map2=new HashMap<>();
        try {
            SpManager spManager=userMapper.selectById(id);
            map1.put("data",spManager);
            map2.put("msg","查询成功");
            map2.put("status",200);
        } catch (Exception e) {
            map1.put("data",null);
            map2.put("msg","查询失败");
            map2.put("status",201);
        }
        RD rd=new RD(map1,map2);
        return rd;
    }
    @PutMapping("/users/{id}")
    public RD updateUser(@PathVariable int id,@RequestBody SpManager spManager)
    {
        Map map1=new HashMap<>();
        Map map2=new HashMap<>();
        System.out.println("id = " + id);
        System.out.println("spManager = " + spManager);
        try {
            userMapper.updataUser(id,spManager);
            SpManager user=userMapper.selectById(id);
            map1.put("data",user);
            map2.put("msg","修改成功");
            map2.put("status",200);
        } catch (Exception e) {
            map1.put("data",null);
            map2.put("msg","修改失败");
            map2.put("status",201);
        }
        RD rd=new RD(map1,map2);
        return rd;
    }
    @DeleteMapping("/users/{id}")
    public RD deleteUser(@PathVariable int id)
    {
        Map map1=new HashMap<>();
        Map map2=new HashMap<>();
        try {
            userMapper.deleteById(id);
            map1.put("data",null);
            map2.put("msg","删除成功");
            map2.put("status",200);
        } catch (Exception e) {
            map1.put("data",null);
            map2.put("msg","删除失败");
            map2.put("status",201);
        }
        RD rd=new RD(map1,map2);
        return rd;
    }



}
